package com.oodso.say.utils;

import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * 类描述：图片加载
 * 修改备注：
 */
public class FrescoUtils {

    public static void loadImage(String url, SimpleDraweeView simpleDraweeView) {
        if (TextUtils.isEmpty(url)) {
            url = "http://";
        }
        String release = Build.VERSION.RELEASE;
        if (!TextUtils.isEmpty(release) && TextUtils.equals(release, "4.4.2")) {
            url = url.replace("https", "http");
        }
        Uri uri = Uri.parse(url);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setImageRequest(request)
                .setOldController(simpleDraweeView
                        .getController()).build();
        simpleDraweeView.setController(controller);
    }


    public static void loadLocalImage(int resId, SimpleDraweeView simpleDraweeView) {

        Uri uri = Uri.parse("res://com.oodso.ConsumSo/" + resId);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setImageRequest(request)
                .setOldController(simpleDraweeView
                        .getController()).build();
        simpleDraweeView.setController(controller);
    }

    public static void showThumb(Uri uri, SimpleDraweeView draweeView, int w, int h) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(w, h))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setImageRequest(request)
                .setOldController(draweeView.getController())
                .setControllerListener(new BaseControllerListener<ImageInfo>())
                .build();
        draweeView.setController(controller);
    }

    public static void showThumb(String url, SimpleDraweeView draweeView) {
        if (TextUtils.isEmpty(url)) {
            url = "http://";
        }
        String release = Build.VERSION.RELEASE;
        if (!TextUtils.isEmpty(release) && TextUtils.equals(release, "4.4.2")) {
            url = url.replace("https", "http");
        }
        Uri uri = Uri.parse(url);

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(100, 100))
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setImageRequest(request)
                .setOldController(draweeView.getController())
                .setControllerListener(new BaseControllerListener<ImageInfo>())
                .build();
        draweeView.setController(controller);
    }

}
